<?php
namespace Admin\Controller;
use Think\Controller;
class BannerController extends BaseController {
    public function banner_list(){
        $this->display();
	}
	
    public function banner_ajax(){
        //接收搜索的值
        $search=I('get.value');
        //分页
        //每页显示的数据
        $limit=5;
        //查询数据总数
        if(empty($search)){
            //查询所有数据
			$count=M('banner')->count();
        }else{
            $count=M('banner')->where("banner_title Like '%$search%'")->count();
		}
		
		//调用TP分页类
		$page=new \Think\Page($count,$limit);
		//生成分页
		$show=$page->show();
        //排序判断
        $ord=I('get.ord');//接收排序ord
        if($ord==1){
            $order="banner_addtime ASC";
        }else{
            $order="banner_addtime DESC";
        }
        //查询数据
        if(empty($search)){
            //如果没有输入搜索
			$banner=M('banner')
					->order("$order")
					->limit($page->firstRow.','.$page->listRows)
					->select();
        }else{
			$banner=M('banner')
                    ->where("banner_title Like '%$search%'")
					->order("$order")
					->limit($page->firstRow.','.$page->listRows)
					->select();
        }
		$this->assign('show',$show);
		$this->assign('banner',$banner);
		$this->assign('a',$page->firstRow+1);
        $this->display();
	}
	
    public function banner_ajax_title(){
        //处理标题ajax的数据
        if(!empty($_GET['value'])){
            $id=$_GET['id'];
            $value=$_GET['value'];
            $data=array(
                'banner_title'=>$value,
            );
            $res=M('banner')->where("banner_id=$id")->save($data);
            if($res){
                echo 1;die;
            }else{
                echo 2;die;
            }
        }
        $this->display();
	}
	
    public function banner_add(){
    	if(I('post.')){
    		$banner_addtime=!empty($_POST['banner_addtime'])?strtotime($_POST['banner_addtime']):time();
    		$data=array(
    			'banner_title'=>I('post.banner_title'),
    			
    			'banner_addtime'=>$banner_addtime,
                'banner_position'=>I('post.banner_position'),
                'banner_ord'=>I('post.banner_ord'),
    		);
    		if($_FILES['banner_path']['size']){
    			$upload=new \Think\Upload();
	    		$upload->exts=array('jpg','png','gif');
	    		$upload->rootPath='./Upload/';
	    		$upload->savePath='banner/';
	    		$info=$upload->upload();
	    		if($info){
	    			$data['banner_path']=$info['banner_path']['savepath'].$info['banner_path']['savename'];
	    		}
    		}
    		$res=M('banner')->add($data);
    		if($res){
    			$this->success('添加成功',U('Admin/banner/banner_list'));
    			exit();
    		}else{
    			$this->error('数据有误，请重新提交');
    		}
    	}
    	$this->display();
    }
    public function banner_edit(){
    	$aid=I('get.aid');
    	$banner=M('banner')->where("banner_id=$aid")->find();
    	$this->assign('banner',$banner);
    	if(I('post.')){
    		$data=array(
    			'banner_title'=>I('post.banner_title'),
                
                // 'banner_addtime'=>$banner_addtime,
                'banner_position'=>I('post.banner_position'),
                'banner_ord'=>I('post.banner_ord'),
    		);
    		$banner_id=I('post.banner_id');
    		if($_FILES['banner_path']['size']){
    			$oldarr=M('banner')->field("banner_path")->where("banner_id=$banner_id")->find();
    			$path='./Upload/'.$oldarr['banner_path'];
    			if(!empty($oldarr) && file_exists($path)){
    				unlink($path);
    			}
    			$upload=new \Think\Upload();
	    		$upload->exts=array('jpg','png','gif');
	    		$upload->rootPath='./Upload/';
	    		$upload->savePath='banner/';
	    		$info=$upload->upload();
	    		if($info){
	    			$data['banner_path']=$info['banner_path']['savepath'].$info['banner_path']['savename'];
	    		}
    		}
    		$res=M('banner')->where("banner_id=$banner_id")->save($data);
    		if($res){
    			$this->success('编辑成功',U('Admin/banner/banner_list'));
    			exit();
    		}else{
    			$this->error('数据有误，请重新提交');
    			exit();
    		}
    	}
    	$this->display();
    }
    public function banner_del(){
    	$a_id=I('get.a_id');
    	$oldarr=M('banner')->field("banner_path")->where("banner_id=$a_id")->find();
		$path='./Upload/'.$oldarr['banner_path'];
		if(!empty($oldarr) && file_exists($path)){
			unlink($path);
		}
		$res=M('banner')->where("banner_id=$a_id")->delete();
		if($res){
			echo 1;exit();
		}else{
			echo M('banner')->getLastSql();exit();
		}
    }
}